# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.10.2...3.19.4)
project(CloudAppSDK)
set(THIRD_PARTY_DIR ${CMAKE_CURRENT_SOURCE_DIR})
if(ANDROID_ABI STREQUAL "arm64-v8a")
    set (THIRD_PARTY_LIB_DIR ${THIRD_PARTY_DIR}/libs/Arm64/arm64-v8a)
    set (THIRD_PARTY_INC_DIR libs/include)
elseif(ANDROID_ABI STREQUAL "armeabi-v7a")
    set (THIRD_PARTY_LIB_DIR ${THIRD_PARTY_DIR}/libs/Arm32/armeabi-v7a)
    set (THIRD_PARTY_INC_DIR libs/include)
endif()

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

add_definitions(-DSDK_VERSION="22.8.0")

add_definitions(-DANDROID)
add_definitions(-DCONTROL_RESPONSE_ENABLED=1)
add_definitions(-DSOCKET_RECONNECT=1)
add_definitions(-D_LOG_TO_FILE_=0)
add_definitions(-DRECONNECT)
add_definitions(-DUSE_TLS=1)
add_definitions(-DMTRANS_ENABLED=1)

set(CMAKE_VERBOSE_MAKEFILE ON)

SET(CPP_ROOT src/main/cpp)

include_directories(libs/libopus/include)
include_directories(libs/openssl/include/)
include_directories(libs/mtrans/include/)
include_directories(libs)

include_directories(cas_common)
include_directories(cas_stream)
include_directories(cas_service)
include_directories(cas_socket)
include_directories(hwsecure)
include_directories(cas_decoder)

find_library( log-lib log )
find_library( android-lib android)
find_library( egl-lib EGL)

link_directories(${THIRD_PARTY_LIB_DIR})
link_directories(${THIRD_PARTY_LIB_DIR}/../)
add_subdirectory(cas_common)
add_subdirectory(cas_stream)
add_subdirectory(cas_service)
add_subdirectory(cas_socket)
add_subdirectory(hwsecure)
add_subdirectory(cas_decoder)


##添加libssl静态库
add_library(ssl STATIC IMPORTED)
set_target_properties(
        ssl
        PROPERTIES IMPORTED_LOCATION
        ${THIRD_PARTY_LIB_DIR}/../libssl.a)

##添加libcrypto静态库
add_library(crypto STATIC IMPORTED)
set_target_properties(
        crypto
        PROPERTIES IMPORTED_LOCATION
        ${THIRD_PARTY_LIB_DIR}/../libcrypto.a)

add_library(libmtrans STATIC IMPORTED)
set_target_properties(
        libmtrans
        PROPERTIES IMPORTED_LOCATION
        ${THIRD_PARTY_LIB_DIR}/../libmtrans.a)

add_library(libopus SHARED IMPORTED)
set_target_properties(libopus
        PROPERTIES IMPORTED_LOCATION
        ${THIRD_PARTY_LIB_DIR}/libopus.so)


file(GLOB controller_src CasJniBridge.cpp CasOpusBridge.cpp CasController.cpp stdErr.cpp)
add_library(cloudapp SHARED ${controller_src})

target_link_libraries(cloudapp
        ssl
        crypto
        libmtrans
        cas_service
        cas_stream
        cas_common
        cas_socket
        ${android-lib}
        ${log-lib}
        libopus
        VideoDecoder
        hwsecure
        )